package com.hjm.mall.config;

import com.hjm.mall.config.exception.TokenVerifyException;
import com.hjm.mall.util.Result;
import com.hjm.mall.util.ResultGenerator;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(TokenVerifyException.class)
    @ResponseBody
    public Result handleTokenVerifyException(TokenVerifyException e) {
        // 构造一个带有错误信息的Result对象
        Result result = ResultGenerator.genErrorResult(401, e.getMessage());
        return result;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleAllException(Exception e) {
        Result result = ResultGenerator.genErrorResult(500, "服务器错误 " + e.getMessage());
        return result;
    }

}
